const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: app.globalData.userInfo,
    navHeight: app.globalData.navHeight,
    capsule: app.globalData.capsule,
    showmodal: false,   
    myTreasures: [],
    selected_treasures: [],
    message: "",



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadTreasure();
  },


  /**
   * 选择宝箱
   */
  selectIcon: function (e) {
    this.setData({
      selected_icon: e.currentTarget.dataset.id
    });
  },

  /**
   * 添加、移除藏入宝物
   */
  /* 获取玩家现有宝物数据*/

  loadTreasure() {
    wx.cloud.callFunction({
      name: 'treasure',
      data: {
        op: 'getMyTreasure'
      }
    }).then(res => {
      console.log('myTreasure', res.result)
      this.setData({
        myTreasures: res.result
      })
    })
  },

  /* 添加宝物*/
  selectOne: function (e) {

    let myTreasures = this.data.myTreasures;
    let selected_treasures = this.data.selected_treasures;
    let index = e.currentTarget.dataset.index
    console.log(myTreasures)
    let xx = this.moveArrayObj(myTreasures, selected_treasures, index, 'type', 'amount')
    this.setData({
      selected_treasures: xx[1],
      myTreasures: xx[0],
    })
  },
  /* 添加宝物*/
  removeOne(e) {
    let selected_treasures = this.data.selected_treasures;
    let myTreasures = this.data.myTreasures;
    let index = e.currentTarget.dataset.index
    let selected_treasure = selected_treasures[index]


    let xx = this.moveArrayObj(selected_treasures, myTreasures, index, 'type', 'amount')
    this.setData({
      myTreasures: xx[1],
      selected_treasures: xx[0],
    })
  },

  moveArrayObj(arrayA, arrayB, index, key1, key2) {
    //A-->B,第index元素，key1是比较相同的元素，key2是数量增加减少的元素

    let arrayItem = arrayA[index]
    console.log(index)
    console.log(arrayItem)
    //A减少
    if (arrayItem[key2] - 1 < 0) {
      wx.showToast({
        title: '这个宝物没有了',
        icon: "none",
      });
      // this.toast.showFailure('这个宝物没有了')
      return false;
    }
    arrayItem[key2]--
    if (arrayItem[key2] == 0) {
      arrayA.splice(index, 1)
    }
    console.log(arrayA)
    // //B增加
    var exists = false;
    arrayB.some(item => {
      if (item[key1] === arrayItem[key1]) {
        item[key2] = item[key2] + 1;
        exists = true;
        return false;
      }
    })

    if (!exists) {
      let newArrayItem = Object.assign({}, arrayItem)
      newArrayItem[key2] = 1
      console.log('new', newArrayItem)
      arrayB.push(newArrayItem)
    }

    // console.log(arrayB)
    // //排序
    arrayB.sort(this.compare(key1));

    let data = [arrayA, arrayB]

    return data
    // console.log(data)

  },

  /* 宝物从新排序，按照type顺序*/
  compare(key) {
    return function (value1, value2) {
      var val1 = value1[key];
      var val2 = value2[key];
      return val1 - val2;
    }
  },
  /**
   * 宝箱留言
   */
  msgInput: function (e) {
    let value = e.detail.value
    if (value.length >= 50) {
      wx.showToast({
        title: '输入字数不能超过50个',
        icon: 'none'
      })
    }
    this.setData({
      message: value
    });
  },
  /**
   * save
   */
  save: function () {
    let selected_treasures = this.data.selected_treasures
    if (selected_treasures.length == 0) {

      wx.showToast({
        title: '需要放入宝物哦',
        icon: 'none'
      })
      return false;
    }
    selected_treasures.some(item => {
      //   delete item._id
      delete item._openid
      delete item.src
      delete item.price
      delete item.title
      delete item.desc
    })

    console.log('selected', selected_treasures)

    wx.cloud.callFunction({
      name: 'box',
      data: {
        op:'hideBox',
        treasures: selected_treasures,
        message: this.data.message
      }
    }).then(res => {
      console.log(res)
      wx.navigateBack()
    })
   
  },



  /**
   * 点击modal空白区域
   */
  modalClose: function () {
    this.setData({
      showmodal: false
    });
  },

  showModal() {
    this.setData({
      showmodal: true
    });
  },

})